package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.common.R;
import com.itheima.domian.Category;
import com.itheima.domian.Dish;
import com.itheima.domian.Flavor;
import com.itheima.domian.ShoppingCart;
import com.itheima.domian.vo.DishVo;
import com.itheima.domian.vo.QueryVo;
import com.itheima.mapper.admin.DishMapper;
import com.itheima.mapper.client.ShoppingCartMapper;
import com.itheima.service.admin.CategoryService;
import com.itheima.service.admin.DishService;
import com.itheima.service.admin.FlavorService;
import com.itheima.service.client.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author 12441
 * @Date 2022/8/18 14:52
 * @Version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private FlavorService flavorService;


    @GetMapping("/list")
    public R<List<DishVo>> statusToStop(Dish dish){
        LambdaQueryWrapper<Dish> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());
        log.info("===========================");
        //排序
        queryWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        List<Dish> list = dishService.list(queryWrapper);
        List<DishVo>dishVoList=list.stream().map((item)->{
            DishVo dishVo = new DishVo();
            BeanUtils.copyProperties(item,dishVo);

            String categoryId = item.getCategoryId();

            Category category = categoryService.getById(categoryId);

            if (category!=null){
                String name = category.getName();
                dishVo.setCategoryName(name);
            }
            Long disId = item.getId();
            LambdaQueryWrapper<Flavor> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(Flavor::getDishId,disId);
            List<Flavor> flavorList = flavorService.list(lambdaQueryWrapper);
            dishVo.setFlavors(flavorList);

            return dishVo;

        }).collect(Collectors.toList());



        return  R.success(dishVoList);
    }

}